# include "scope.h"
# include "filePathSettings.h"

int main(int argc, char *argv[])
{
    RC_udp::readXML();

    QApplication *app = new QApplication(argc, argv);

    scope m_scope;
    m_scope.dispInfo("等待连接");

    cpp_tools::csvRecorder plotData_recorder(host_path + data_path + "/plotData/plotData.csv");

    bool isRuning = true;
    std::thread t_udp([&]() -> void {
        cpp_tools::udpVecReceiver<double> plotData_receiver(udpSettings::PORT::upper_lower::plot_len,udpSettings::PORT::upper_lower::plot,1);

        double t_last = cpp_tools::getCurrentTimeAsDouble();
        while (isRuning)
        {
            double t_now = cpp_tools::getCurrentTimeAsDouble();

            if (plotData_receiver.receive() == 0)
            {
                m_scope.dispInfo("正常");

                // std::cout << "update time " << t_now - t_last << std::endl;

                t_last = t_now;

                m_scope.addPlotData(plotData_receiver.get());

                m_scope.dispDelay(plotData_receiver.get()(Eigen::last));

                plotData_recorder.record(plotData_receiver.get());
            }

            if (t_now - t_last >= 0.5)
            {
                m_scope.dispInfo("断开连接");
            }

            std::this_thread::sleep_for(std::chrono::milliseconds(1));
        }
    });

    m_scope.show();

    int info = app->exec();
    isRuning = false;
    t_udp.join();
    return info;
}
